In [9]:
from bokeh.io import output_notebook, show
from bokeh.plotting import figure
In [10]:
output_notebook()
There are many places where you may need to specify colors. Bokeh can accept colors in a variety of different ways:
'green'
, 'indigo'
'#FF0000'
, '#44444444'
In [ ]:
In [3]:
# EXERCISE Create a plot of your own and customize several plot-level properties
To style axes, you first must get ahold of Axis
objects. The simplest way is to use some convenience methods on Plot
: axis, xaxis, and yaxis. These methods return lists of axis objects:
>>> p.xaxis
[<bokeh.models.axes.LinearAxis at 0x106fa2390>]
However, you can set properties on all the elements of the list as if it was a single object:
p.xaxis.axis_label = "Temperature"
p.axis.major_label_text_color = "orange"
These are referred to as "splattable" lists, and tab completion works on them as well.
In [7]:
# EXERCISE Try out tab completion. Type p.xaxis.<press tab key> to see a list of attributes that can be set.
axis_label_standoff
orientation
major_tick_in
major_tick_out
minor_tick_in
minor_tick_out
As a simple first example, let's change the orientation of the major tick labels on both axes of a plot:
In [11]:
from math import pi
p = figure(plot_width=400, plot_height=400)
p.x([1,2,3,4,5], [2,5,8,2,7], size=10, line_width=2)
p.xaxis.major_label_orientation = pi/4
p.yaxis.major_label_orientation = "vertical"
show(p)
The next example shows customizations on several of the different Axis properties at once:
In [13]:
p = figure(plot_width=400, plot_height=400)
p.asterisk([1,2,3,4,5], [2,5,8,2,7], size=12, color="olive")
# change just some things about the x-axes
p.xaxis.axis_label = "Temp"
p.xaxis.axis_line_width = 3
p.xaxis.axis_line_color = "red"
# change just some things about the y-axes
p.yaxis.axis_label = "Pressure"
p.yaxis.major_label_text_color = "orange"
p.yaxis.major_label_orientation = "vertical"
# change things on all axes
p.axis.minor_tick_in = -3
p.axis.minor_tick_out = 6
show(p)
In [7]:
# EXERCISE Create a plot of your own and customize several axis properties
There are further customizations possible. See the User Guide for more information on topics such as tick label formatting or limiting axis bounds.
In [16]:
p = figure(plot_width=400, plot_height=400)
p.circle([1,2,3,4,5], [2,5,8,2,7], size=10)
# change just some things about the x-grid
p.xgrid.grid_line_color = None
# change just some things about the y-grid
p.ygrid.grid_line_alpha = 0.5
p.ygrid.grid_line_dash = [6, 4]
show(p)
In [5]:
p = figure(plot_width=400, plot_height=400)
p.circle([1,2,3,4,5], [2,5,8,2,7], size=10)
# change just some things about the x-grid
p.xgrid.grid_line_color = None
# change just some things about the y-grid
p.ygrid.band_fill_alpha = 0.1
p.ygrid.band_fill_color = "navy"
show(p)
In [10]:
# EXERCISE Create a plot of your own and customize several grid properties
In [15]:
import numpy as np
x = np.linspace(0, 4*np.pi, 100)
y = np.sin(x)
p = figure()
p.circle(x, y, legend="sin(x)")
p.line(x, y, legend="sin(x)")
p.line(x, 2*y, legend="2*sin(x)", line_dash=[4, 4], line_color="orange", line_width=2)
p.line(x, 3*y, legend="3*sin(x)", line_color="green")
p.square(x, 3*y, legend="3*sin(x)", fill_color="white", line_color="green")
p.legend.orientation = "bottom_left"
show(p)
In [12]:
# EXERCISE Create a plot of your own and add a legend